<template>
  <div id="questions">
    <a-form :model="page" layout="inline">
      <a-form-item field="title" label="题目id" style="min-width: 300px">
        <a-input v-model="page.questionId" placeholder="请输入题目id" />
      </a-form-item>
      <a-form-item field="language" label="编程语言">
        <a-select
          :style="{ width: '320px' }"
          placeholder="选择编程语言"
          v-model="page.language"
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button @click="doSearch">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0"></a-divider>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        total: total,
        pageSize: page.pageSize,
        current: page.current,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #judgeInfo="{ record }">
        {{ record.judgeInfo.message }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";

const dataList = ref();
const page = ref({
  language: undefined,
  questionId: undefined,
  current: 1,
  pageSize: 10,
  sortField: "createTime",
});
const total = ref(0);
const searchQuestion = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    page.value
  );
  console.log("返回结果：" + res);
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败," + res.message);
  }
};
onMounted(() => {
  searchQuestion();
});

//相当于监听searchQuestion中的所有变量,当其中任何一个变量改变的时候就会执行
watchEffect(() => {
  searchQuestion();
});

const columns = [
  {
    title: "提交号",
    dataIndex: "id",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "状态",
    dataIndex: "status",
  },

  {
    title: "题目id",
    dataIndex: "questionId",
  },
  {
    title: "提交人id",
    dataIndex: "userId",
  },
  {
    title: "提交时间",
    dataIndex: "createTime",
  },
];

const onPageChange = (pageCurrent: number) => {
  page.value = {
    ...page.value,
    current: pageCurrent,
  };
};

//进行搜索
const doSearch = () => {
  page.value = {
    ...page.value,
    current: 1, //将当前页设置为1
  };
};
</script>

<style>
#questions {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
